Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add violations backend code #447

Merged
merged 11 commits into from
May 10, 2022
Merged

Conversation

AndrewLester
Copy link
Contributor

@AndrewLester AndrewLester commented Apr 23, 2022

Summary

Closes: #428

Changes

  • Adds three new endpoints:
  • Violation reasons list
  • Violations clear
  • Violations add

Requests / Responses

GET `/projects/violation-reasons` Returns violation reasons

Response

[{"id":"862a8a5a-78a6-491c-808c-568b784d7743","description":"DID AN OOPSIE!"}]
PUT `/projects/:uuid/clear-violations` Clears a project's violations

Request
No body

Response

"Success"
POST `/projects/:uuid/add-violation` Adds a violation

Request

{"reasons": [{"id": "862a8a5a-78a6-491c-808c-568b784d7743"}]}

Response

{"id":"37e88233-b1dd-40fe-9783-fdfbcabc215a","reasons":[{"id":"862a8a5a-78a6-491c-808c-568b784d7743","description":"DID AN OOPSIE!"}],"creator":"62ab5343-36d4-449b-9c2f-2c796cae27ff","date":"2022-04-28T02:46:47.186095Z","project":"542c8806-5db9-4890-9bea-2b0ad2b94c5c"}

@AndrewLester AndrewLester marked this pull request as ready for review April 28, 2022 02:55
Comment on lines +610 to +631
class ProjectViolationsClearApiView(APIView):
"""
Clear all violations for a project

Requires authentication and owner.
Returns project details.
"""

permission_classes = [IsAuthenticated, IsOwner]
throttle_classes = [CustomUserRateThrottle, SustainedRateThrottle]

def post(self, request, *, pk):
try:
old = Project.objects.get(pk=pk)
except Project.DoesNotExist:
return Response('Not found', status=404)

if not request.user or old.creator.id != request.user.id:
return Response('Error', status=403)

old.violations.set([])
old.save()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we add this functionality to the update function in the ProjectSerializer? This would also prevent the user from having to make two api calls on the frontend when updating a project.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but I feel like that's going to be replaced pretty quickly in the future when actual moderation is implemented. This system is more extensible imo. I feel like 2 API calls isn't much of a problem, but I can replace this if we're sure.

Comment on lines 635 to 653
class ProjectViolationAddApiView(CreateAPIView):
"""
Creates a new violation and adds it to a project.

Requires authentication and staff or moderator.
Returns the violation details.
"""
queryset = Violation.objects.all()
serializer_class = ViolationSerializer
permission_classes = [IsAuthenticated, IsStaffOrModerator]
throttle_classes = [PostUserRateThrottle, SustainedRateThrottle]

def perform_create(self, serializer):
pk = self.kwargs.get("pk")
project = Project.objects.get(id=pk)
violation = serializer.save(creator=self.request.user, project=project)
project.violations.add(violation)
project.save()
return violation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Send notification to user to let them know their project was unpublished.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean we need a new notification type?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah

@ajain1921
Copy link
Contributor

Can you also pre-load the Violations table like the Categories table is?

@ajain1921 ajain1921 changed the base branch from master to unpublish_project April 29, 2022 22:34
@abetco abetco added the h4i Hack4Impact issue label May 3, 2022
@ajain1921 ajain1921 merged commit 5d6f366 into unpublish_project May 10, 2022
@ajain1921 ajain1921 deleted the al/violations-backend branch May 10, 2022 04:00
ajain1921 added a commit that referenced this pull request May 10, 2022
* Init

* Unpublish project form (#452)

* Lay out structure

* Make progress

* Style menu

* Style checkboxes & buttons

* Add api call

* Make mobile-friendly

* Last styling changes

* Address styling comments

* Remove for non-staff & non-moderators

* Add violations backend code (#447)

* Add violations backend system

* Fix typo

* Add new migrations

* Basic

* Use array

* Fix issuesgit status

* Swap fetch parameter

* Add responses

* Add new violation ui for violation notifications

* Fix models

Co-authored-by: Aditya Jain <[email protected]>

* Unpublished Project Modal (#446)

* set up modal

* style changes

* change to label and mobile styles

* tiny fix of class style

* scrolling

* tiny style fix

* clean up unused things

* overflow scroll bar adjustments

* Small style

* remove important

Co-authored-by: Aditya Jain <[email protected]>

* Integrate

Co-authored-by: Grace Zhang <[email protected]>
Co-authored-by: Andrew Lester <[email protected]>
Co-authored-by: Zora Zhang <[email protected]>
NdibeRaymond pushed a commit that referenced this pull request Jun 5, 2022
* Init

* Unpublish project form (#452)

* Lay out structure

* Make progress

* Style menu

* Style checkboxes & buttons

* Add api call

* Make mobile-friendly

* Last styling changes

* Address styling comments

* Remove for non-staff & non-moderators

* Add violations backend code (#447)

* Add violations backend system

* Fix typo

* Add new migrations

* Basic

* Use array

* Fix issuesgit status

* Swap fetch parameter

* Add responses

* Add new violation ui for violation notifications

* Fix models

Co-authored-by: Aditya Jain <[email protected]>

* Unpublished Project Modal (#446)

* set up modal

* style changes

* change to label and mobile styles

* tiny fix of class style

* scrolling

* tiny style fix

* clean up unused things

* overflow scroll bar adjustments

* Small style

* remove important

Co-authored-by: Aditya Jain <[email protected]>

* Integrate

Co-authored-by: Grace Zhang <[email protected]>
Co-authored-by: Andrew Lester <[email protected]>
Co-authored-by: Zora Zhang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
h4i Hack4Impact issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Project Unpublished Backend Support
3 participants